//
//  GameDetailViewController.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2021/12/30.
//

#import "GameDetailViewController.h"
#import "GameDetailNewViewModel.h"

#import "GameDetailScrollView.h"
#import "GameDetailTopView.h"
#import "GameDetailPageViewController.h"
#import "GamePublishCommentsViewController.h"

@interface GameDetailViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewBottom;
@property (weak, nonatomic) IBOutlet UIButton *bottomBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomCommentView;


@property(nonatomic,strong) GameDetailNewViewModel *viewModel;
@property (nonatomic, strong) GameDetailScrollView *scrollView;
@property (nonatomic, strong) GameDetailTopView *topView;
@property (nonatomic, strong) GameDetailPageViewController *pageVc;



@end

@implementation GameDetailViewController


- (void)setupUI
{
    VHWeakSelf
    self.bottomViewBottom.constant = -UIDevice.vg_safeDistanceBottom;
    [self.bottomCommentView addTapBlock:^(id  _Nonnull obj) {
        VHStrongSelf
        [self commentClick];
    }];
    
    self.scrollView = [[GameDetailScrollView alloc] init];
    self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.mas_equalTo(-(UIDevice.vg_safeDistanceBottom + 70));
    }];
    self.scrollView.scrollViewBlock = ^(CGFloat scroll_y) {
        VHStrongSelf
        [self scrollViewDidScrollBlock:scroll_y];
    };
    UIView *scrollContentView = [[UIView alloc] init];
    [self.scrollView addSubview:scrollContentView];
    [scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.centerX.equalTo(self.scrollView);
    }];
    
    self.topView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([GameDetailTopView class]) owner:self options:nil].firstObject;
    [scrollContentView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(scrollContentView);
        make.height.mas_equalTo([GameDetailTopView height]);
    }];
    [self requestData:scrollContentView];
    
    
}

-(GameDetailNewViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[GameDetailNewViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.backBtn setImage:[UIImage imageNamed:@"ic_detail_toolbar_like_orange"] forState:UIControlStateSelected];
    [self scrollViewDidScrollBlock:0];
    
}

- (void)setModelId:(NSString *)modelId
{
    _modelId = modelId;
}

- (void)requestData:(UIView *)scrollContentView
{
    VHWeakSelf
    [self.viewModel fetchDetailWithDetailId:_modelId block:^{
        VHStrongSelf
        self.centerTitle.text = self.viewModel.detailModel.gameName;

        self.topView.viewModel = self.viewModel;
        
        self.pageVc = [[GameDetailPageViewController alloc] init];
        self.pageVc.viewModel = self.viewModel;
        __weak typeof(self) weakSelf0 = self;
        self.pageVc.pageIndexBlock = ^(NSInteger index) {
            __strong typeof(self) self = weakSelf0;
            self.scrollView.currentIndex = index;
        };
        [self addChildViewController:self.pageVc];
        [scrollContentView addSubview:self.pageVc.view];
        [self.pageVc.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topView.mas_bottom);
            make.left.right.bottom.equalTo(scrollContentView);
            make.height.mas_equalTo(SL_WINDOW_HEIGHT - UIDevice.vg_navigationFullHeight - UIDevice.vg_safeDistanceBottom - 70);
        }];
    }];
    
}

#pragma mark - UIScrollViewBlock
- (void)scrollViewDidScrollBlock:(CGFloat)scroll_y {
    if (scroll_y > 100) {
        self.naviView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
        [self.backBtn setImage:[UIImage imageNamed:@"ic_detail_toolbar_back_black"] forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"ic_detail_toolbar_like_black"] forState:UIControlStateNormal];
        self.centerTitle.hidden = NO;
        self.bottomLine.backgroundColor = kLineColor;
    } else {
        self.naviView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
        [self.backBtn setImage:[UIImage imageNamed:@"ic_detail_toolbar_back_white"] forState:UIControlStateNormal];
        [self.rightBtn setImage:[UIImage imageNamed:@"ic_detail_toolbar_like_white"] forState:UIControlStateNormal];
        self.centerTitle.hidden = YES;
        self.bottomLine.backgroundColor = [UIColor clearColor];
    }
    
}


- (void)rightBtnClick
{
    self.rightBtn.selected = !self.rightBtn.selected;
}

- (IBAction)bottomClick:(id)sender {
    NSLog(@"开始游戏");
}

- (void)commentClick {
    NSLog(@"评论");
}



@end
